package com.jk.blog.util;

import java.security.MessageDigest;
import com.alibaba.fastjson.JSON;

public class StringUtils {

    public static final String EMPTY = "";

    /**
     * 字符MD5加密
     * @param inStr 源字符
     * @return      加密字符
     */
    public static String toMD5(String inStr){
        if(isEmpty(inStr))
            return null;
        MessageDigest md5;
        try{  
            md5 = MessageDigest.getInstance("MD5");  
        }catch (Exception e){  
            System.out.println(e.toString());  
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    }

    /**
     * 判断字符为空
     * @param source
     * @return true-为空；false-非空
     */
    public static boolean isEmpty(String source){
        if(source == null)
            return true;
        return source.isEmpty();
    }


    /**
     * 判断两个字符值是否相等
     * @param str1 比较对象1
     * @param str2 比较对象2
     * @return  true-相等；false-不等。
     */
    public static boolean isEqual(String str1, String str2){
        if(str1 != null && str1.equals(str2))
            return true;
        return false;
    }

    /**
     * 判断两个字符值不等
     * @param str1  比较对象1
     * @param str2  比较对象2
     * @return  true-不等；false-相等。
     */
    public static boolean isNotEqual(String str1, String str2){
        if(isEqual(str1, str2))
            return false;
        return true;
    }

    /**
     * 字符format
     * @param str
     * @param args
     * @return
     */
    public static String formatStr(String str , Object...args){
        StringBuffer sb =new StringBuffer();
        String[] params = getStrArrayStr(str , args);
        if(args != null && args.length >0){
            for (int i = 0; i < params.length; i++) {
                if(params[i].indexOf("{"+i)>0){
                    sb.append(params[i].replace("{"+i, args[i] == null ?"null":JSON.toJSONString(args[i])));
                }else{
                    sb.append(params[i]);
                }
            }
        }
        return sb.toString();
    }

    public static String[] getStrArrayStr(String str , Object...args){
        if(StringUtils.isEmpty(str)){
            throw new RuntimeException("params is empty");
        }
        String[] params= str.split("}");
        if(params == null || params.length == 0){
            throw new RuntimeException("str is empty");
        }

        if(str.endsWith("}")){
            if(params.length !=args.length ){
                System.out.println("str:"+str+"reqeust:"+JSON.toJSONString(args));
                throw new RuntimeException("参数数量不一致");
            }
        }else{
            if(params.length-1 !=args.length ){
                System.out.println("str:"+str+"reqeust:"+JSON.toJSONString(args));
                throw new RuntimeException("参数数量不一致");
            }
        }
        return params;
    }
}
